/**
 * 消息推送管理初始化
 */
var PushMsg = {
    id: "PushMsgTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    isAdmin: false
};

/**
 * 初始化表格的列
 */
PushMsg.initColumn = function () {
    return [[
        {title: '应用名称', field: 'appName',  align: 'center'},
        {title: '标题', field: 'title', align: 'center'},
        {title: '内容', field: 'content',
            align: 'center', templet:function (row) {
                var content = row.content;
                return "<a onclick=\"preview('"+content.split('\n')+"')\">"+content+"</a>";
            }},
        {title: '创建时间', field: 'createTime', align: 'center'},
        {title: '状态', field: 'status', align: 'center', templet: function (row) {
                if(row.status == '1'){
                    return "已发送"
                }
                else if(value == "0"){
                    return "未发送"
                }
            }}
    ]];
};

PushMsg.initAdminColumn = function () {
    return [[
        {title: '商户名称', field: 'merchantName', align: 'center'},
        {title: '应用名称', field: 'appName',  align: 'center'},
        {title: '标题', field: 'title', align: 'center'},
        {title: '内容', field: 'content',
            align: 'center', templet:function (row) {
                var content = row.content;
                return "<a onclick=\"preview('"+content.split('\n')+"')\">"+content+"</a>";
            }},
        {title: '创建时间', field: 'createTime', align: 'center'},
        {title: '状态', field: 'status', align: 'center', templet: function (row) {
                if(row.status == '1'){
                    return "已发送"
                }
                else if(row.status == "0"){
                    return "未发送"
                }
            }}
    ]];
};


function preview(row){
    var c = row.split(",");
    var len = c.length;
    var str="";
    for (var i = 0; i < len; i++) {
        str += c[i] + "<br/>"
    }
    var content = '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">'+str+'</div>';
    layer.open({
        type: 1
        ,title: false //不显示标题栏
        ,closeBtn: false
        ,area: '500px;'
        ,shade: 0.8
        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
        ,btn: ['关闭']
        ,btnAlign: 'c'
        ,moveType: 1 //拖拽模式，0或者1
        ,content: content
        ,success: function(layero){

        }
    });
}

PushMsg.refresh = function(){
    PushMsg.table.reload("tableReload",{
        page: {
            curr: 1 //重新从第1页开始
        },
    });
}

PushMsg.tableList = function (queryData) {
    layui.use("table", function () {
        var table = layui.table;

        PushMsg.table = table;

        table.render({
            elem: "#table",
            url: Feng.ctxPath + "/pushMsg/list",
            cellMinWidth: 120,
            where: queryData,
            page: true,
            limit: 20,
            cols: PushMsg.isAdmin?PushMsg.initAdminColumn():PushMsg.initColumn(),
            parseData: function (res) {
                return {
                    "code": 0,
                    "msg": "获取成功",
                    "count": res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        });
    });
};

$(function () {
    if ($("#isAdmin").val() == "超级管理员"){
        PushMsg.isAdmin = true;
    }else{
        PushMsg.isAdmin = false;
    }
    PushMsg.tableList();
});
